<?php
    namespace Home\Controller;
    use Think\Controller;
    class HischaxunController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
        $attr = I("get.attr");
        $begin_time = I("get.begin_time");
        $end_time = I("get.end_time");
        $type = I("get.type");
        $type_1 = I("get.type_1");
		$admin_uuid = I('admin_uuid');
		$admin_dept_list_str = get_user_dept_list($admin_uuid);
		
        if(empty($type))
        {
            $type = I("post.type");
        }

        if(empty($begin_time))
        {
            $begin_time = I("post.begin_time");
        }

        if(empty($end_time))
        {
            $end_time = I("post.end_time");
        }

        if(empty($begin_time))
        {
            $begin_time = 0;
        }
        else
        {
            $begin_time = strtotime($begin_time);
        }

        if(empty($end_time))
        {
            $end_time = 9999999999;
        }
        else
        {
            $end_time = strtotime($end_time);
        }

        if(empty($type))
        {

            $type = 1;
        }

        if(empty($type_1))
        {

            $type_1 = 1;
        }

        if($type == 1)
        {
            $keshi = "ORDERED_BY_DEPT";
        }
        else
        {
            $keshi = "PERFORMED_BY_DEPT";
        }

        if($type_1 == 1)
        {
            $tujing = "OUTP_CLASS2_INCOME";
        }
        else
        {
            $tujing = "INP_CLASS2_INCOME";
        }



        $sql = "SELECT DECODE (GROUPING (b.dept_code), 1, 'HJH', b.dept_code)
            AS dept_code,
         DECODE (GROUPING (b.dept_code),
                 1, '合计',
                 MIN (NVL (b.dept_name, b.dept_code)))
            AS \"科室名称\",
         SUM (charges) \"合计\",
         
    ";
        
        $sql_list = "";

        $list = M("cbhs.cbhs_distribution_calc_schm")->select();
        for ($i=0; $i < count($list); $i++) 
        { 
            $sql_list .= "SUM (DECODE (a.reck_class, '".$list[$i]["item_class"]."', charges, 0)) \"".$list[$i]["item_class_name"]."\",";
        }

        $sql_list = rtrim($sql_list,",");
        
        $sql .= $sql_list;

        $sql .= "FROM HISFACT.$tujing a, hisdata.dept_dict b
		 WHERE st_date >= $begin_time
         AND st_date <= $end_time
         AND b.is_stop != 0
         AND a.".$keshi." = b.dept_code(+)
		 AND b.dept_code in ($admin_dept_list_str)
GROUP BY ROLLUP (b.dept_code)
ORDER BY b.dept_code NULLS FIRST ";
        
        // echo $sql;
        // die();

        $data = M()->query($sql);

        for ($i=0; $i < count($data); $i++) 
        { 
            unset($data[$i]['dept_code']);
        }

        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("hisdata.outp_bill_items")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);

        $result = M("hisdata.outp_bill_items")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("hisdata.outp_bill_items")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function get_sys_dept_dict()
    {
        $attr = I('post.attr');

        $where = " 1 = 1 ";

        if(!empty($attr))
        {
            $where .= " and attr = $attr";
        }

        $list = M("comm.sys_dept_dict")->where("")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_col_list()
    {
        $col = [];
        $col[] = "合计";
        $col[] = "科室名称";
        $list = M("cbhs.cbhs_distribution_calc_schm")->select();
        for ($i=0; $i < count($list); $i++) 
        { 
            $col[] = $list[$i]["item_class_name"];
        }
        $json["data"] = $col;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }


}